

// window.onload=function (){
//     const xhr = new XMLHttpRequest();
//     xhr.open("get", 'http://http://localhost:8080//query?',true)
//     xhr.send()
//     xhr.onload = function () {
//         let total1=0
//         let total2=0
//         let show=document.getElementById("show")
//         let j=JSON.parse(xhr.responseText)
//         for (var i = 0; i < j.length; i++){
//             total1=parseFloat(total1)+parseFloat(j[i].outPrice)*parseInt(j[i].number)
//             let li=(parseFloat(j[i].outPrice)-parseFloat(j[i].inPrice))*parseInt(j[i].number)
//             total2=parseFloat(total2)+parseFloat(li)
//             show.innerHTML+="<tr style=\"border: solid;\">" +
//                 "<td style=\"border: solid;\">"+j[i].name+"</td>" +
//                 "<td style=\"border: solid;\">"+j[i].inPrice+"</td>" +
//                 "<td style=\"border: solid;\">"+j[i].outPrice+"</td>" +
//                 "<td style=\"border: solid;\">"+j[i].number+"</td>" +
//                 "<td style=\"border: solid;\">"+li+"</td>" +
//                 "<td style=\"border: solid;\">"+j[i].time.substring(0,10)+"</td>" +
//                 "<td><button id='"+j[i].id+"' onclick='del(this.id)'>删除</button></td>"+
//                 "<td><button id='"+j[i].id+"' onclick='fix(this.id)'>修改</button></td>"+
//                 "</tr>"
//         }
//         show.innerHTML+="<tr><td>总零售</td><td>"+total1+"</td></tr>"
//         show.innerHTML+="<tr><td>总利润</td><td>"+total2+"</td></tr>"
//     }
// }

document.getElementById("btn").onclick=function (){
    let name=$('#name').val()
    let inPrice=$('#inPrice').val()
    let outPrice=$('#outPrice').val()
    let number=$('#number').val()
    let time=$('#time').val()
    const xhr = new XMLHttpRequest();
    xhr.open("get", 'http://localhost:8080/insert?name='+name+'&inPrice='+inPrice+'&outPrice='+outPrice+'&number='+number+'&time='+time,true)
    xhr.send()
    xhr.onload = function () {
        location.reload();
    }
}
document.getElementById("btn1").onclick=function (){
    let name=document.getElementById("name1").value
    let time= document.getElementById('time1').value+'-'+document.getElementById('time2').value
    const xhr = new XMLHttpRequest();
    xhr.open("get", 'http://localhost:8080/query?name='+name+'&time='+time,true)
    xhr.send()
    xhr.onload = function () {
        let total1=0
        let total2=0
        let show=document.getElementById("show")
        show.innerHTML=""
        show.innerHTML+="<tr style=\"border: solid;\">\n" +
            "            <th style=\"border: solid;\">商品名</th>\n" +
            "            <th style=\"border: solid;\">进价</th>\n" +
            "            <th style=\"border: solid;\">零售价</th>\n" +
            "            <th style=\"border: solid;\">数量</th>\n" +
            "            <th style=\"border: solid;\">利润</th>\n" +
            "            <th style=\"border: solid;\">销售时间</th>\n" +
            "        </tr>"
        let j=JSON.parse(xhr.responseText)
        for (var i = 0; i < j.length; i++){
            total1=parseFloat(total1)+parseFloat(j[i].outPrice)*parseInt(j[i].number)
            let li=(parseFloat(j[i].outPrice)-parseFloat(j[i].inPrice))*parseInt(j[i].number)
            total2=parseFloat(total2)+parseFloat(li)
            show.innerHTML+="<tr style=\"border: solid;\">" +
                "<td style=\"border: solid;\">"+j[i].name+"</td>" +
                "<td style=\"border: solid;\">"+j[i].inPrice+"</td>" +
                "<td style=\"border: solid;\">"+j[i].outPrice+"</td>" +
                "<td style=\"border: solid;\">"+j[i].number+"</td>" +
                "<td style=\"border: solid;\">"+li+"</td>" +
                "<td style=\"border: solid;\">"+j[i].time.substring(0,10)+"</td>" +
                "<td><button id='"+j[i].id+"' onclick='del(this.id)'>删除</button></td>"+
                "<td><button id='"+j[i].id+"' onclick='fix(this.id)'>修改</button></td>"+
                "</tr>"
        }
        show.innerHTML+="<tr style=\"border: solid;\"><td>总零售</td><td>"+total1+"</td></tr>"
        show.innerHTML+="<tr style=\"border: solid;\"><td>总利润</td><td>"+total2+"</td></tr>"
    }
}
function del(t){
    let m= prompt("请输入密码")
    const xhr = new XMLHttpRequest();
    xhr.open("get", 'http://localhost:8080/del?id='+t+'&m='+m,true)
    xhr.send()
    xhr.onload = function () {
        alert(JSON.parse(xhr.responseText))
    }
    location.reload();
}
function fix(t){
    const xhr1 = new XMLHttpRequest();
    xhr1.open("get", 'http://localhost:8080/query?id='+t,true)
    xhr1.send()
    xhr1.onload = function () {
        let show=document.getElementById("show")
        let j=JSON.parse(xhr1.responseText)
        show.innerHTML=""
        show.innerHTML+=
            "<tr>商品名<input id='fix-name' value='"+j[0].name+"'></tr>"+
            "<tr>进价<input id='fix-inPrice' value='"+j[0].inPrice+"'></tr>"+
            "<tr>零售价<input id='fix-outPrice' value='"+j[0].outPrice+"'></tr>"+
            "<tr>数量<input id='fix-number' value='"+j[0].number+"'></tr>"+
            "<tr>销售时间<input id='fix-time' type='date' value='"+j[0].time+"'></tr>"
            + "<tr><button id='"+j[0].id+"'  onclick='save(this.id)'>保存</button></tr>"
    }

}
function save(t){
    let name= $("#fix-name").val()
    let inPrice= $("#fix-inPrice").val()
    let outPrice= $("#fix-outPrice").val()
    let number= $("#fix-number").val()
    let time= $("#fix-time").val()
    const xhr2 = new XMLHttpRequest();
    xhr2.open("get", 'http://localhost:8080/fix?name='+name+'&inPrice='+inPrice+'&outPrice='+outPrice+'&number='+number+'&time='+time+'&id='+t,true)
    xhr2.send()
    xhr2.onload = function () {
        alert(JSON.parse(xhr2.responseText))
    }
    location.reload();
}